package com.imcrl.core.config
{
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;

    [ExcludeClass]
    public class Configuration
    {

        /**
         * PREFIX 包 locaname。
         * 如果需要修改xml，可在此处修改
         */
        public static const PREFIXS:String = 'prefixes';

        /**
         * service 包localname;
         */		
        public static const SERVICES:String ='services';
        /**
         * 资源 tag localname
         */
        public static const RESOURCES:String ='resources';
	
        private static var sectionRegs:Dictionary;

       
        public function Configuration()
        {
            configuration = new Dictionary();
            if(sectionRegs==null){
                sectionRegs=new Dictionary();
                sectionRegs[PREFIXS]=PrefixSection;
                sectionRegs[RESOURCES]=ResourceSection;
                sectionRegs[SERVICES]=ServiceSection;
            }
        }

        /**
         * 注册自定义节点解析;
         * @param sectionName
         * @param parserClass
         *
         */		
        public function registSectionParser(sectionName:String,parserClass:Class):void{
            sectionRegs[sectionName]=parserClass;
        }


        public function get config():Dictionary{
            return configuration;
        }

        private  var configuration:Dictionary;

		private var _version:String;
		public function get version():String{
			return _version;
		}
		
        /**
         * 初始化 配置文件
         * @param xml
         *
         */
        public  function initializeConfiguration(xml:XML,version:String):void
        {
			_version=version;
            var localName:String;
            for each(var element:XML  in xml.elements())
            {

                localName=element.localName();

                var list:Array;
                if(sectionRegs[localName]){
                    var seccls:Class=sectionRegs[localName];
                    list=getSectionsByXML(element,seccls);
                }else{
                   trace('不存在命令解析');
                    continue;
                }

                var dic:Dictionary=configuration[localName];
                if(dic==null){
                    dic=configuration[localName]=new Dictionary();
                }

                for each(var item:AbstractSection in list){
                    if(dic[item.name]){
						trace('xml node '+item.name+' exist');
                    }

                    dic[item.name]=item;
                }
            }
        }


        /**
         * 由二进制文件直接反出配置信息;
         * @param bytes
         *
         */		
        public function initByteArray(bytes:ByteArray):void{
            configuration=bytes.readObject();
        }


        private function getSectionsByXML(xml:XML,sectionClass:Class):Array{
            var list:Array=[];

            var se:AbstractSection;
            for each(var ele:XML in xml.children()){

                se=new sectionClass();
                se.config=this;
                se.bindXML(ele);

                list.push(se);
            }

            return list;
        }

        /**
         * 获取item的url
         * @param nodeName
         * @param attributeName
         * @return
         *
         */
        public function getItemURL(nodeName:String, attributeName:String):String
        {
            if((nodeName in configuration)==false){
                return '';
            }
            var dic:Dictionary =configuration[nodeName];
            if((attributeName in dic)==false){
                return '';
            }
            return dic[attributeName].value;
        }


        public function getConfig(nodeName:String):Dictionary{
            return configuration[nodeName];
        }

        public function setPrefixValue(name:String,value:String):void
        {
            var prefixSection:PrefixSection = configuration[PREFIXS][name];

            prefixSection.value= value;

        }

    }
}

